<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-21
 * Time: 上午9:22
 */
class Login extends MY_Controller {
    protected $login=array(
        array(
            'field' => 'phone',
            'label' => '帐号',
            'rules' => 'trim|required|numeric|is_mobile|xss_clean|callback_checkLoginPhone'
        ),
        array(
            'field' => 'pwd',
            'label' => '密码',
            'rules' => 'trim|required|xss_clean|callback_checkPwd'
        )
    );
    protected $code=array(
        array(
            'field' => 'phone',
            'label' => '手机号码',
            'rules' => 'trim|required|numeric|is_mobile|xss_clean|callback_checkLoginPhone|callback_checkNum'
        ),
        array(
            'field' => 'captchas',
            'label' => '图形验证码',
            'rules' => 'trim|required|xss_clean|callback_captchas_verify'
        ),
        array(
            'field' => 'code',
            'label' => '手机验证码',
            'rules' => 'trim|required|numeric|xss_clean|callback_smscode_check'
        ),
    );
    protected $getcode=array(
        array(
            'field' => 'phone',
            'label' => '手机号码',
            'rules' => 'trim|required|numeric|is_mobile|xss_clean|callback_checkLoginPhone|callback_checkNum'
        ),
        array(
            'field' => 'captchas',
            'label' => '图形验证码',
            'rules' => 'trim|required|xss_clean|callback_captchas_verify'
        )
    );
    function __construct(){
        parent::__construct();

    }
    public function index()
    {
        if($this->uid) redirect(site_url('article'));
        $this->vdata['header'] =array(
            'title'=> $this->mcfg->get_config('site','title_seo'),
            'tags'=> $this->mcfg->get_config('site','tags'),
            'intro' => $this->mcfg->get_config('site','intro')
        );
        # 在使用控制器时输出CI共模板使用
        $this->vdata['CI'] =& $this;
        $this->load->view('login', $this->vdata);
    }
    public function go() {
        if($this->uid) redirect(site_url('article'));
        $this->form_validation->set_rules($this->login);
        if ($this->form_validation->run() == false) {
            $this->vdata['status'] = 0;
            $this->vdata['msg'] = form_errors();
        } else {
            $data = $this->input->post();
            $uphone=$this->macc->get_one_site(array('phone'=>$data['phone'],'pwd'=>passwd($data['pwd'])),'id,uname,pwd');
            $this->load->helper('funs');
            $session = array(
                "uid" => $uphone['id'],
                "uname" =>$uphone['uname'] ,
            );
            $this->session->set_userdata($session);
            $this->vdata['status'] = 1;
            $this->vdata['msg'] = '登录成功';
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
    }
    public function haveCode() {
        if($this->uid) redirect(site_url('article'));
        $this->form_validation->set_rules($this->code);
        if ($this->form_validation->run() == false) {
            $this->vdata['status'] = 0;
            $this->vdata['msg'] = form_errors();
        } else {
            $data = $this->input->post();
            $uphone=$this->macc->get_one(array('phone'=>$data['phone']),'id,uname,pwd');
            $this->load->helper('funs');
            $session = array(
                "uid" => $uphone['id'],
                "uname" =>$uphone['uname'] ,
            );
            $this->session->set_userdata($session);
            $this->vdata['status'] = 1;
            $this->vdata['msg'] = '登录成功';
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($this->vdata));
    }
    public function checkLoginPhone($uname){
        if($this->uid) redirect(site_url('article'));
        $userinfo = $this->macc->get_one_site(array('phone'=>$uname,'pwd'=>passwd($_POST['pwd'])));
        if($uname && $userinfo){
            if($userinfo['audit']) {
                $this->form_validation->set_message('checkLoginPhone', '您的帐号已被禁用' );
                return false;
            } else {
                if($userinfo['status'] == 0) {
                    $this->form_validation->set_message('checkLoginPhone', '您的帐号审核未通过' );
                    return false;
                } elseif($userinfo['status'] == 1) {
                    $this->form_validation->set_message('checkLoginPhone', '您的账户将在24小时内审核' );
                    return false;
                } else {
                    $this->load->model('identity_model','midentity');
                    $identity = $this->midentity->get_one(array('uid'=>$userinfo['id']));
                    //审核状态0提交审核1审核不通过2审核通过
                    if($identity) {
                        if(!$identity['status']) {
                            $this->form_validation->set_message('checkLoginPhone', '您的帐号身份变更正在审核中' );
                            return false;
                        } elseif($identity['status'] == 1) {
                            $this->form_validation->set_message('checkLoginPhone', '您的帐号身份变更审核不通过' );
                            return false;
                        }
                    }

                }

            }
            return true;
        } else {
            $this->form_validation->set_message('checkLoginPhone', '此手机号不存在' );
            return false;
        }
    }
    public function checkPwd($pwd)
    {
        if($this->uid) redirect(site_url('article'));
        $data = $this->input->post('phone');
        $user = $this->macc->get_one(array('phone'=>$data));
        if(!$user) {
            $this->form_validation->set_message('checkPwd', '此手机号不存在');
            return FALSE;
        } else {
            if($user['audit']) {
                $this->form_validation->set_message('checkPwd', '您的帐号已被禁用');
                return FALSE;
            }
        }
        $pwd = passwd($pwd);
        if ($pwd == $user['pwd']) {
            return TRUE;
        } else {
            $this->form_validation->set_message('checkPwd', '密码错误，请确认后登录');

            return FALSE;
        }
    }
    public function code_check($value = '') {
        if($this->uid) redirect(site_url('article'));
        $code = $this->session->userdata('code');
        $data = $this->input->post();
        $this->load->model('limit_model','limit');
        if(ENVIRONMENT=='development'){
            $code_check = '1234';
        } else {
            $code_check = $this->limit->check_phone_code($data['phone'],$data['code']);
        }

        if ( $value == $code || $code_check) {
            return TRUE;
        }else{
            $this->form_validation->set_message('code_check', '手机验证码错误,请检查,或者重新获取');
            return FALSE;
        }
    }
    public function getPhoneCode(){
        if($this->uid) redirect(site_url('article'));
        if($this->input->is_ajax_request() and is_post()){
            $this->load->model('limit_model','mlimit');
            $this->form_validation->set_rules($this->getcode);
            if ($this->form_validation->run() == false){
                $vdata['status']=0;
                $vdata['msg']=form_errors();
            }else{
                //发送短信--写入session
                $data=$this->input->post();
                $this->load->model('smscode_model', 'msmscode');
                if ($content = $this->msmscode->sendCode($data['phone'], 0,1)) {
                    $code = $content['auth_code'];
                    $vdata['status'] = 1;
                    $vdata['msg'] = '短信验证码已发送，请注意查收';
                }

            }
        }else{
            show_404();
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    protected function sendCode($phone){
        if($this->uid) redirect(site_url('article'));
        $code=mt_rand(1000,9999);
        //发送短信
        //$msg="您的短信验证码是:".$code;
        // $fsurl="http://120.26.69.248/msg/HttpSendSM?account=shiyuan_mige&pswd=Migexk888&mobile=".$phone."&msg=".$msg."&needstatus=true";
        // $res=file_get_contents($fsurl);
        //$vdata['msg']='验证码已发送，请注意查收';$vdata['status']=1;
        $this->session->set_userdata('phone_code',$code);
        return $code;
    }
    public function logout()
    {
        $this->session->sess_destroy();
        redirect(site_url('login'));
    }
    public function smscode_check($smscode) {
        $this->load->model('smscode_model', 'msmscode');
        if (!$this->msmscode->verifySmsCode($_POST['phone'], $smscode, 0)) {
            // 返回失败
            $this->form_validation->set_message('smscode_check', '验证码验证失败,请重新填写');
            return false;
        } else {
            return true;
        }


        return true;
    }
}
